package com.ls.config.auth;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;

/**
 * @author pengyu
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class SaTokenConfigure implements WebMvcConfigurer {

    private final ObjectMapper objectMapper;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {


        // 注册 Sa-Token 拦截器
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 登录校验 -- 拦截所有路由，并排除登录接口
            SaRouter
                    .match("/**")
                    .notMatch("/login/**")
                    .notMatch("/customerApp/getInfo")
                    .notMatch("/customers/getInfo")
                    .notMatch("/images/**")
                    .notMatch("/bannerApp/**")
                    .notMatch("/alipayApp/notify")
                    .notMatch("/weixinApp/notify")
                    .notMatch("/inviteH5/**")
                    .notMatch("/companyH5/**")
                    .notMatch("/bookingH5/**")
                    .notMatch("/startBannerApp/**")
                    .notMatch("/versionApp/**")
                    .notMatch("/inviteTemplateH5/**")
                    .notMatch("/exampleApp/**")
                    .notMatch("/exampleH5/**")
                    .check(r -> {
                        StpUtil.checkLogin();
                    });

            // 四个角色 超级管理员:admin 平台客服:staff 会员:member 非会员:common
//            SaRouter.match("//**", r -> StpUtil.checkRoleAnd("admin"));
//            SaRouter.match("//**", r -> StpUtil.checkRoleOr("member","common"));
            SaRouter.match("/customer/**", r -> StpUtil.checkRoleOr("admin", "staff"));
//            SaRouter.match("/customer/**", r -> StpUtil.checkRoleAnd("staff"));
//            SaRouter.match("//**", r -> StpUtil.checkRoleOr("admin","staff","member","common"));

            SaRouter.match("/loginRecord/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/activity/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/activityApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/activityUser/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/aiApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/alipay/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/alipayApp/advancePay", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/alipayApp/getPayStatus", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/area/**", r -> StpUtil.checkRoleOr("admin", "staff","member", "common"));
            SaRouter.match("/banner/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/benefit/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/benefitApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/bookingApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/bookingTime/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/bookingTimeApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/bookNoteApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/boothApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/companyApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/company/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/customer/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/customerApp/updateOwnInfo", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/customerApp/setPlatformSign", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/customerApp/setSign", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/customerApp/setBlackList", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/customerApp/cancelBlackList", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/customerApp/setPassword", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/customerApp/getUserInfo", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/customerApp/getUserSign", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/example/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/feedbackRecord/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/feedbackRecordApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/holidayApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/holiday/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/holidayCategory/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/holidayCategoryApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/invite/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/inviteApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/inviteTemplate/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/isSend/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/label/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/labelApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/linkmanApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/logistic/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/orderRecord/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/orderRecordApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/phoneRecord/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/phoneRecordApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/thali/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/thaliApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/smsRuleApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/scoreRecord/**", r -> StpUtil.checkRoleOr("admin","member", "common"));
            SaRouter.match("/seatingApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/sms/**", r -> StpUtil.checkRoleOr( "member", "common"));
            SaRouter.match("/smsRecord/**", r -> StpUtil.checkRoleOr("admin", "staff","member", "common"));
            SaRouter.match("/startBanner/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/group/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/groupApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/smsTemplate/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/smsTemplateApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/user/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/version/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/weixin/**", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/weixinApp/advancePay", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/weixinApp/getPayStatus", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/yxSms/**", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/yxSmsApp/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/blackList/**", r -> StpUtil.checkRoleOr("member", "common"));
            SaRouter.match("/logistic/pageList", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/logistic/import", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/logistic/send", r -> StpUtil.checkRoleOr("admin"));
            SaRouter.match("/logistic/queryTrack", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/logistic/getStatus", r -> StpUtil.checkRoleOr("admin", "staff"));
            SaRouter.match("/trade/**", r -> StpUtil.checkRoleOr("member", "common"));


        })).addPathPatterns("/**");
    }


}
